/**
 * 
 */
package org.statewalker.tokenizer.base;

import org.statewalker.tokenizer.stream.ICharStream;
import org.statewalker.tokenizer.stream.ICharStream.IMarker;
import org.statewalker.tokenizer.stream.ICharStream.Pointer;

/**
 * @author kotelnikov
 */
public abstract class AbstractTokenizer<T> implements ITokenizer<T> {

    protected ITokenFactory<T> fFactory;

    /**
     * 
     */
    public AbstractTokenizer(ITokenFactory<T> factory) {
        fFactory = factory;
    }

    protected T newToken(ICharStream stream, IMarker marker) {
        Pointer begin = marker.getPointer();
        Pointer end = stream.getPointer();
        int len = end.len(begin);
        String str = marker.getSubstring(len);
        return newToken(begin, end, str);
    }

    /**
     * Returns a newly created token.
     * 
     * @param begin the start position of the token
     * @param end the end position of the token
     * @param str the string representation of the token
     * @return a newly created token
     */
    protected final T newToken(Pointer begin, Pointer end, String str) {
        return fFactory.newToken(begin, end, str);
    }

}
